﻿using System;
using System.Windows;
using System.Windows.Controls;

namespace BigPictureExample
{
	/// <summary>
	/// A very simple silverlight control demonstrating how to wire up mouse interactions to a MultiScaleImage
	/// instance.
	/// </summary>
	public partial class Page : UserControl
	{
		/// <summary>
		/// Stores an instance of the wrapper class that handles mouse interactions (panning, zooming, etc)
		/// </summary>
		private MultiScaleImageWrapper mouseInteractionWrapper;

		/// <summary>
		/// Indicates if the first motion has completed yet (the initial zoom behavior is disabled)
		/// </summary>
		private bool isInitialMotionFinished;

		public Page()
		{
			InitializeComponent();

			// initialize the wrapper class to enable mouse interactions
			mouseInteractionWrapper = new MultiScaleImageWrapper(image);
		}

		/// <summary>
		/// Wires up an absolute Uri to the MultiScaleImage collection path (for some reason, relative values
		/// are not currently supported). This should make this example work, no matter what port is auto-assigned
		/// by Cassini.
		/// </summary>
		/// <param name="sender">The MultiScaleImage instance.</param>
		/// <param name="e">Unused RoutedEvent arguments.</param>
		private void image_Loaded(object sender, RoutedEventArgs e)
		{
			Uri collectionUri;

			if (Uri.TryCreate(App.Current.Host.Source, "/Collection/items.bin", out collectionUri))
				image.Source = collectionUri;
		}

		/// <summary>
		/// Handles the "MotionFinished" event fired by the MultiScaleImage, but only re-enables the "UseSprings"
		/// property after the first motion completes (a little trick to properly bypass the initial "zoom in
		/// from nowhere" animation when first loading)
		/// </summary>
		/// <param name="sender">The MultiScaleImage instance.</param>
		/// <param name="e">Unused RoutedEvent arguments.</param>
		void image_InitialMotionFinished(object sender, RoutedEventArgs e)
		{
			if (!isInitialMotionFinished)
			{
				isInitialMotionFinished = true;
				image.UseSprings = true;
			}
		}
	}
}